'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminYydjCtrl
 * @description
 * # AdministratorAdminYydjCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorAdminYydjCtrl', function ($scope, DataService, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        $scope.chooseinfo = function () {
            var relModuleObj = {
                name: "AdministratorAdminYydjCtrl",
                selectTab: "select_seal_by_use",
                selectTabContent: "印章字样"
            };
            // 清掉关联的编号
            var params = $scope.param;
            Util.savaParamForReview(params, id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        };
        $scope.showfileType = function () {
            var param = {}
            param.type = "用印文件类型";
            DataService.getData(openApi.getKeyType, param).then(function (data) {
                $scope.fileTypeList = data
            })
        };
        $scope.swipe = function (index) {
            $scope.index = index;
        }
        $scope.resetlist = function (index) {
            $scope.index = -1;
        }
        //获取证件信息列表
        $scope.getChoosecertId = function () {
            Util.saveSelectLists($scope.param.detailList);
            Util.savaParamForReview($scope.param, id);
            $location.path("administrator/chooseinfo/yysq");
        };
        // 删除证件信息列表
        $scope.deletedItem = function (index) {
            $scope.param.detailList.splice(index, 1);
            $scope.index = -1;
            Util.saveSelectLists($scope.param.detailList);
        };
        var getAllpkIds = function () {
            var pkIds = [],
                pkIdsStr;
            $scope.param.detailList.forEach(function (item, index) {
                pkIds.push(item.pkId);
            });
            pkIdsStr = pkIds.join(",");
            return pkIdsStr;
        }
        $scope.submit = function (operationId) {

            $scope.param.sealIds = getAllpkIds();
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.useSeal;
            options.param = angular.copy($scope.param, {});
            delete options.param.detailList
            options.param.fileType = $scope.param.fileTypeArr.toString();
            FormSubmit.submit(options, $scope);
        };
        $scope.validate = function () {
            if (!$scope.param.sealText) {
                Util.Toast("请选择印章字样");
                return;
            }
            if (!($scope.param.fileTypeArr && $scope.param.fileTypeArr.length > 0)) {
                Util.Toast("请选择用印文件类型");
                return;
            }
            if (!$scope.param.useReason) {
                Util.Toast("请填写用印事由");
                return;
            }
            if (!$scope.param.times) {
                Util.Toast("请填写份数");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }
        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                // 获取选择页面返回的数据并且合并到参数中
                var selectLists;
                selectLists = Util.getSelectLists();
                $scope.param.detailList = selectLists || $scope.param.detailList || [];
                $scope.param.fileTypeArr = $scope.param.fileTypeArr ? $scope.param.fileTypeArr : ($scope.param.fileType && $scope.param.fileType.split(",") || [])
            });
            $scope.showfileType()
        };
        // 初始化
        init();

    });
